package com.mall.server.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.commons.utils.PageUtils;
import com.mall.dubbo.api.UmsMenuApi;
import com.tanhua.model.domain.UmsMenu;
import com.tanhua.model.vo.PageVo;
import com.tanhua.model.vo.UmsMenuVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UmsMenuService {

    @DubboReference
    private UmsMenuApi umsMenuApi;

    //分页查询后台菜单
    public PageVo listGetById(Integer pageNum, Integer pageSize, Integer parentId) {

        Page page = umsMenuApi.listGetById(pageNum, pageSize, parentId);
        if (page == null) {
            throw new BusinessException(ErrorResult.emptyError());
        }
        return PageUtils.getPageVo(page);

    }

    //根据ID获取菜单详情
    public UmsMenu getById(Integer id) {
        UmsMenu menu = umsMenuApi.getById(id);
        if (menu == null) {
            throw new BusinessException(ErrorResult.emptyError());
        }
        return menu;
    }

    //修改菜单显示状态
    public void updateHiddenById(Integer id, Integer hidden) {
        boolean exits = umsMenuApi.updateHiddenById(id, hidden);
        if (!exits) {
            throw new BusinessException(ErrorResult.emptyError());
        }
    }

    //根据ID删除后台菜单
    public void deletes(Integer id) {
        boolean exits = umsMenuApi.deletes(id);
        if (!exits) {
            throw new BusinessException(ErrorResult.EmaError());
        }

    }

    // 添加后台菜单
    public void creates(UmsMenu umsMenu) {
        boolean exits = umsMenuApi.creates(umsMenu);
        if (!exits) {
            throw new BusinessException(ErrorResult.EmaError());
        }
    }

    //树形结构返回所有菜单列表
    public List<UmsMenuVo> treeList() {
        //全都数据,父类和子类
        List<UmsMenu> umsMenus = umsMenuApi.treeListAll();

        //存放子类的容器
        ArrayList<UmsMenuVo> VosZi = new ArrayList<>();
        ArrayList<UmsMenuVo> VosFu = new ArrayList<>();

      /*  //遍历所有数据,把父类和子类分开
        List<UmsMenuVo> VosFu = umsMenus.stream().map(findAll -> {
            UmsMenuVo menuVo = new UmsMenuVo();
            BeanUtils.copyProperties(findAll, menuVo);
            if (menuVo.getParentId() == 0) {
                //获取父类,并存放VosFu
                return menuVo;
            }
            //把子类存放VosZi
            VosZi.add(menuVo);
            return null;
        }).collect(Collectors.toList());*/

        for (UmsMenu umsMenu : umsMenus) {
            UmsMenuVo menuVo = new UmsMenuVo();
            BeanUtils.copyProperties(umsMenu, menuVo);
            if (menuVo.getParentId() == 0) {
                //获取父类,并存放VosFu
                VosFu.add(menuVo);
            }
            VosZi.add(menuVo);
        }


        //创建一个map用来存放子类对象,以及pid
        Map<Long, ArrayList<UmsMenuVo>> map = new HashMap<>();
        //循环子类对象(VosZi)
        for (UmsMenuVo menuVo : VosZi) {
            //获取 parentid
            Long parentId = menuVo.getParentId();

            //通过map.get(parentId) 看看有没有这个对应的key
            if (map.get(parentId) == null) {
                ArrayList<UmsMenuVo> list = new ArrayList<>();
                list.add(menuVo);
                //parentId 为key  list为value
                map.put(parentId, list);
            } else {
                //key已经存在,继续存入对应的value
                ArrayList<UmsMenuVo> list2 = map.get(parentId);
                //把list集合继续存入里边
                list2.add(menuVo);
            }
        }

        //递归
        List<UmsMenuVo> digui = digui(VosFu, map);
        return digui;
    }

    /**
     * @param vosFu
     * @param map
     * @return
     */
    private List<UmsMenuVo> digui(List<UmsMenuVo> vosFu, Map<Long, ArrayList<UmsMenuVo>> map) {
        //结束条件
        if (map.size() == 0) {
            return null;
        }
        for (UmsMenuVo umsMenuVo : vosFu) {
            if (umsMenuVo == null) {
                break;
            }
            Long id = umsMenuVo.getId();
            //通过map.get(id)去如果不等于空就进来
            if (map.get(id) != null) {
                umsMenuVo.setChildren(map.get(id));
                //用过的id删除掉,防止错误
                map.remove(id);
                //防止下边还有子级,继续递归
                digui(vosFu, map);
            }

        }

        return vosFu;
    }

    //修改后台菜单
    public void updatess(UmsMenu umsMenu) {
        boolean exits = umsMenuApi.updatess(umsMenu);
        if (!exits) {
            throw new BusinessException(ErrorResult.emptyError());
        }
        return;

    }
}
